<template>
  <div class="app-container">
    <!-- 顶部导航 -->
    <div class="top-navbar">
      <div class="nav-tabs">
        <el-radio-group v-model="activeTab" size="medium">
          <el-radio-button label="客户" />
          <el-radio-button label="类别" />
          <el-radio-button label="供应商" />
          <el-radio-button label="商品" />
          <el-radio-button label="支出" />
          <el-radio-button label="收入" />
        </el-radio-group>
      </div>

      <div class="search-area">
        <el-input
          v-model="listQuery.keyword"
          placeholder="输入类别名称查询"
          clearable
          style="width: 200px"
        />

        <el-select v-model="listQuery.status" placeholder="显示启用类别" style="width: 150px; margin-left: 10px">
          <el-option label="显示启用类别" value="enabled" />
          <el-option label="显示禁用类别" value="disabled" />
          <el-option label="显示全部类别" value="all" />
        </el-select>

        <el-button type="primary" class="search-btn" @click="handleFilter">查询</el-button>
      </div>

      <div class="operation-buttons">
        <el-button type="primary" @click="handleAdd">新增</el-button>
        <el-button @click="handleDisable">禁用</el-button>
        <el-button @click="handleEnable">启用</el-button>
        <el-button @click="handleRefresh">刷新</el-button>
      </div>
    </div>

    <!-- 表格区域 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="加载中"
      border
      fit
      highlight-current-row
      header-cell-style="background-color: #f5f7fa; color: #333;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)">
            <i class="el-icon-edit" />
          </el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row)">
            <i class="el-icon-delete" />
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="类别" prop="name" align="center" />

      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enabled"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <div v-if="total > 0" class="pagination-container">
      <el-pagination
        background
        :current-page.sync="listQuery.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 新增/编辑分类对话框 -->
    <el-dialog
      :title="dialogStatus === 'create' ? '新增客户分类' : '编辑客户分类'"
      :visible.sync="dialogFormVisible"
      width="500px"
    >
      <el-form ref="categoryForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'CustomerCategory',
  data() {
    return {
      activeTab: '客户',
      listLoading: false,
      list: [
        { id: 1, name: '广东省内', enabled: true },
        { id: 2, name: '华北地区', enabled: true },
        { id: 3, name: '零售', enabled: true },
        { id: 4, name: '批发', enabled: true },
        { id: 5, name: '华东地区', enabled: true },
        { id: 6, name: '批发', enabled: true },
        { id: 7, name: '华南地区', enabled: true },
        { id: 8, name: '华中地区', enabled: true }
      ],
      total: 8,
      listQuery: {
        page: 1,
        limit: 10,
        keyword: '',
        status: 'enabled'
      },
      dialogFormVisible: false,
      dialogStatus: 'create',
      form: {
        id: undefined,
        name: '',
        enabled: true
      },
      rules: {
        name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      multipleSelection: []
    }
  },
  created() {
    // this.getList();
    this.getCustomerCategoryList()
  },
  methods: {
    getList() {
      this.listLoading = true
      // 模拟API请求
      setTimeout(() => {
        let filtered = [...this.list]

        if (this.listQuery.keyword) {
          filtered = filtered.filter(item =>
            item.name.toLowerCase().includes(this.listQuery.keyword.toLowerCase())
          )
        }

        if (this.listQuery.status !== 'all') {
          const isEnabled = this.listQuery.status === 'enabled'
          filtered = filtered.filter(item => item.enabled === isEnabled)
        }

        this.total = filtered.length

        // 分页
        const start = (this.listQuery.page - 1) * this.listQuery.limit
        const end = start + this.listQuery.limit
        this.filteredList = filtered.slice(start, end)

        this.listLoading = false
      }, 500)
    },
    getCustomerCategoryList() {
      this.listLoading = true
      this.list = []
      this.total = 0
      this.listLoading = false
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    resetForm() {
      this.form = {
        id: undefined,
        name: '',
        enabled: true
      }
    },
    handleAdd() {
      this.resetForm()
      this.dialogFormVisible = true
    },
    handleAddCategory() {
      this.handleAdd()
    },
    handleEdit(row) {
      this.form = Object.assign({}, row)
      this.dialogFormVisible = true
    },
    // 确定
    submitForm() {

    },
    // 状态改变
    handleStatusChange(row) {
      const statusText = row.enabled ? '启用' : '禁用'
      this.$message({
        message: `已${statusText}${row.name}`,
        type: 'success'
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 模拟删除操作
        const index = this.list.findIndex(v => v.id === row.id)
        this.list.splice(index, 1)
        this.total = this.list.length
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleDisable() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请选择要禁用的分类',
          type: 'warning'
        })
        return
      }

      const selectedIds = this.multipleSelection.map(item => item.id)

      this.list.forEach(item => {
        if (selectedIds.includes(item.id)) {
          item.enabled = false
        }
      })

      this.$message({
        type: 'success',
        message: '禁用成功!'
      })
    },
    handleEnable() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请选择要启用的分类',
          type: 'warning'
        })
        return
      }

      const selectedIds = this.multipleSelection.map(item => item.id)

      this.list.forEach(item => {
        if (selectedIds.includes(item.id)) {
          item.enabled = true
        }
      })

      this.$message({
        type: 'success',
        message: '启用成功!'
      })
    },
    handleRefresh() {
      this.listQuery.keyword = ''
      this.listQuery.status = 'enabled'
      this.getList()
      this.$message({
        message: '刷新成功',
        type: 'success'
      })
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.top-navbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 20px;
}

.nav-tabs {
  display: flex;
  margin-right: 20px;
}

.nav-tabs .el-radio-button__inner {
  border-radius: 0;
}

.nav-tabs .active {
  background-color: #409EFF;
  color: white;
}

.search-area {
  display: flex;
  align-items: center;
  flex-grow: 1;
}

.search-btn {
  margin: 0 15px;
}

.info-btn {
  margin-right: 20px;
  color: #409EFF;
}

.operation-buttons {
  white-space: nowrap;
}

.pagination-container {
  margin-top: 20px;
  text-align: center;
}

.dialog-footer {
  text-align: center;
}
</style>
